//
// Created by Tiany on 2021/6/17.
//
#include "SeqList.h"
#include "stdio.h"
#include "stdlib.h"

//#define MAXNUM 100

//创建空顺序表,n为总表长
PSeqList createNullList_seq(int m) {
    PSeqList pSeqList = (PSeqList) malloc(sizeof(struct SeqList));
    if (pSeqList != NULL) {//如果创建顺序表成功
        //则为表中元素申请空间
        pSeqList->element = (struct DataType *) malloc(sizeof(struct DataType) * m);
        if (pSeqList->element) {//元素空间申请成功，则进行下一步
            pSeqList->MAXNUM = m;
            pSeqList->n = 0;
            return pSeqList;
        } else free(pSeqList);
    }
    printf("空间分配失败");
    return NULL;
}

//将数据写入顺序表
PSeqList writeList_seq(PSeqList pSeqList, struct DataType x[], int length) {
    if (length > pSeqList->MAXNUM) {
        printf("顺序表长度不够！");
        return NULL;
    }
    for (int i = 0; i < length; ++i) {
        pSeqList->element[i].n = x[i].n;
        pSeqList->n++;
    }
    return pSeqList;
}

//判断是否为空顺序表
int isNullList_seq(PSeqList pSeqList) {
    return (pSeqList->n);
}

//在顺序表中求元素x的下标
int locate_seq(PSeqList pSeqList, struct DataType x) {
    for (int i = 0; i < pSeqList->n; i++) {
        if (pSeqList->element[i].n == x.n) {
            return i;
        }
    }
    return -1;
}

//在下标p的元素之前插入一个新的元素x
int insertPre_seq(PSeqList pSeqList, int p, struct DataType x) {
    int q;
    if (pSeqList->n >= pSeqList->MAXNUM) {
        printf("元素个数大于表长，数据溢出");
        return 0;
    }
    if (p < 0 || p > pSeqList->n) {
        printf("不存在该下标");
        return 0;
    }
    for (q = pSeqList->n - 1; q >= p; q--) {
        pSeqList->element[q + 1].n = pSeqList->element[q].n;
    }
    pSeqList->element[p].n = x.n;
    pSeqList->n++;
    return 1;
}

//在下标为p元素后插入元素x
int insertPost_seq(PSeqList pSeqList, int p, struct DataType x) {
    int q;
    if (pSeqList->n >= pSeqList->MAXNUM) {
        printf("元素个数大于表长，数据溢出");
        return 0;
    }
    if (p < 0 || p > pSeqList->n) {
        printf("不存在该下标");
        return 0;
    }
    for (q = pSeqList->n - 1; q >= p; q--) {
        pSeqList->element[q + 2].n = pSeqList->element[q + 1].n;
    }
    pSeqList->element[p + 1].n = x.n;
    pSeqList->n++;
    return 1;
}

//删除下标为p的元素
int deleteP_seq(PSeqList pSeqList, int p) {
    int q;
    if (p < 0 || p > pSeqList->n - 1) {
        printf("不存在下标为%d的元素", p);
        return 0;
    }
    for (q = p; q < pSeqList->n - 1; q++) {
        pSeqList->element[q].n = pSeqList->element[q + 1].n;
    }
    pSeqList->n--;
    return 1;
}

//删除元素x
int deleteV_seq(PSeqList pSeqList, struct DataType x) {
    return deleteP_seq(pSeqList, locate_seq(pSeqList, x));
}

//删除所有元素x
int deleteAllV_seq(PSeqList pSeqList, struct DataType x) {
    int bool = locate_seq(pSeqList, x);/*在顺序表中查找元素的下标*/
    while (bool != -1) {/*能够查找到元素下标，则说明没删除完毕，继续删除*/
        deleteP_seq(pSeqList, bool);/*删除下标对应元素*/
        bool= locate_seq(pSeqList,x);
    }
    return 0;
}

//获取数组长度 函数无法实现，弃用
/*int getDataLength(Data x[]) {
    printf("%d  ++%d", sizeof(x), sizeof(x[0]));
    return (sizeof(x[]) / sizeof(x[0]));
}*/

//读取并打印顺序表
void readList_seq(PSeqList pSeqList) {
    if (pSeqList != NULL) {
        printf("当前表长为：%d 表中元素为：", pSeqList->n);
        for (int i = 0; i < pSeqList->n; ++i) {
            printf("%d  ", pSeqList->element[i].n);
        }
    }

}

//将顺序表逆置
void sortSeq(PSeqList pSeqList) {
    struct DataType tmp;
    if (pSeqList != NULL) {
        for (int i = 0; i <= pSeqList->n / 2 - 1; i++) {
            tmp.n = pSeqList->element[i].n;
            pSeqList->element[i].n = pSeqList->element[pSeqList->n - 1 - i].n;
            pSeqList->element[pSeqList->n - 1 - i].n = tmp.n;
        }
    }
}